package com.lightdz.tshirtdz.bll;

import com.lightdz.tshirtdz.dal.impl.ProductDal;
import com.lightdz.tshirtdz.model.Enum.ProductTypeEnum;
import com.lightdz.tshirtdz.model.PageInfo;
import com.lightdz.tshirtdz.model.api.ApiPStyle;
import com.lightdz.tshirtdz.model.api.ApiProduct;
import com.lightdz.tshirtdz.model.api.ApiResponse;
import com.lightdz.tshirtdz.model.entity.ProductEntity;

import com.lightdz.tshirtdz.utils.LogHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ Description：${description}
 * @ Author     ：zbl
 * @ Date       ：Created in 16:54 09/08/2018
 * @ Modified By：
 */
@Repository
public class ProductBll {

    @Autowired
    ProductDal productDal;
    @Autowired
    BrowserHistoryeBll browserHistoryeBll;


    public static ApiPStyle pStyle = new ApiPStyle();

    /**
     * 获取产品的详细信息
     * @param wxId
     * @param pid
     * @return
     */
    public ApiResponse<ApiProduct> getApiProduct(String wxId, long pid) {

        ApiResponse<ApiProduct> apiResponse = ApiResponse.getNoResponse();

        ApiProduct product = null;
        try {
            product = new ApiProduct();
            ProductEntity entity = productDal.getProduct(pid);

            //region 设置全局 的商品规格
            pStyle.setPid(pid);
            String[] colorsArr = entity.getpColors().split(",");
            pStyle.setColorList(Arrays.asList(colorsArr));
            String[] sizeArr = entity.getpSizes().split(",");
            pStyle.setSizeList(Arrays.asList(sizeArr));
            pStyle.setWxId(wxId);
            //endregion

            //TODO 此处应用多线程做
            //region 添加到我的浏览记录

            //先判断浏览记录是否存在
            boolean exist = browserHistoryeBll.ExistBrowseHistroy(wxId, pid);
            if (!exist) {
                //如果不存在，则新加浏览记录，保存30天
                browserHistoryeBll.addBrowser(wxId, pid);
            } else {
                //如果存在，则更新浏览记录
                browserHistoryeBll.updateBrowserTime(wxId, pid);
            }

            //endregion

            product.setBannerPicUrl(entity.getpBannerPicUrl());
            product.setDesc(entity.getpDesc());
            product.setName(entity.getpName());
            product.setPrice(entity.getpPrice());
            product.setPid(pid);

            apiResponse = new ApiResponse<ApiProduct>("OK", product, true);
        } catch (Exception e) {
            e.printStackTrace();
            LogHelper.error("ProductBll.getApiProduct" ,e);
        }
        return apiResponse;
    }

    /**
     * 得到商品的规格
     * @param pid
     * @return
     */
    public ApiResponse<ApiPStyle> getPStyle(long pid) {
        return new ApiResponse<>(pStyle) ;
    }

    /**
     * 根据商品类型获取制定的商品列表。如果不指定类型，则获取全部的商品
     * @param productType
     * @param pageInfo
     * @return
     */
    public ApiResponse<List<ApiProduct>> getProductList(ProductTypeEnum productType, PageInfo pageInfo) {
        ApiResponse<List<ApiProduct>> apiResponse = ApiResponse.getNoResponse();

        List<ApiProduct> result = null;
        try {
            result = new ArrayList<>();

            List<ProductEntity> productList = productDal.getProductList(productType, pageInfo);
            if (!productList.isEmpty()) {
                for (ProductEntity pro : productList) {

                    ApiProduct product = new ApiProduct();
                    product.setBannerPicUrl(pro.getpBannerPicUrl());
                    product.setDesc(pro.getpDesc());
                    product.setName(pro.getCreater());
                    product.setPid(pro.getPid());
                    product.setPrice(pro.getpPrice());
                    product.setProductType(ProductTypeEnum.valueOf(pro.getpType()));

                    result.add(product);
                }
            }
            apiResponse=new ApiResponse<>(result);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            LogHelper.error("ProductBll.getProductList" ,e);
        }
        return apiResponse;
    }
}
